package controller;

/*
 * Action for researcher menu, such as create new survey, view existing survey, etc.
 */

import helper.UserType;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import databeans.MailingList;
import databeans.Survey;
import databeans.User;

import model.MailingListDAO;
import model.Model;
import model.Sid_RespondentDAO;
import model.SurveyDAO;

import org.mybeans.dao.DAOException;

public class ResearcherAction extends Action {
	private SurveyDAO surveyDAO;
	private Sid_RespondentDAO srDAO;
	private MailingListDAO mlDAO;

	public ResearcherAction(Model model) {
		surveyDAO = model.getSurveyDAO();
		srDAO = model.getSid_RespondentDAO();
		mlDAO = model.getMailingListDAO();
	}

	public String getName() {
		return "researcher.do";
	}
    
    public String perform(HttpServletRequest request) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        // Check if user is researcher
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	
    	String button = request.getParameter("button");
    	
    	if (button == null)
    		return "researcher.jsp";
    	
    	try {
        	if (button.equals("View List of Surveys")) {
        		Survey[] userSurveys = surveyDAO.getUserSurveys(user.getUserid());
        		Integer[] respondentnumberlist = new Integer[userSurveys.length];
        		Integer[] completednumberlist = new Integer[userSurveys.length];
        		
        		for (int i = 0; i < userSurveys.length; i++) {
        			respondentnumberlist[i] = srDAO.numRespondents(userSurveys[i].getSid());
        			completednumberlist[i] = srDAO.numCompleted(userSurveys[i].getSid());
        		}
        		
        		synchronized(session) {
	        		session.setAttribute("surveylist", userSurveys);
	        		session.setAttribute("respondentnumberlist", respondentnumberlist);
	        		session.setAttribute("completednumberlist", completednumberlist);
        		}
        		
				return "viewsurvey.jsp";
        	}
        	if (button.equals("Logout")) {
        		synchronized(session) {
        			session.setAttribute("user", null);
        		}
    			return "homepage.jsp";
        	}
        	if (button.equals("Mailing Lists")){
        		MailingList[] mllist = mlDAO.getMailingListByUserid(user.getUserid());
        		synchronized(session) {
        			session.setAttribute("mailinglistbody", "init");
        			session.setAttribute("mailinglist", mllist);
        			session.setAttribute("pageFrom", "researcher.jsp");
        		}
                return "mailinglist.jsp";
        	}
    	} catch (DAOException e) {
    		errors.add(e.getMessage());
    		return "errors.jsp";
    	}
		return "researcher.jsp";
    }
}
